﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimeTableManagement.Entities;
using TimeTableManagement;

namespace TimeTableManagement.Controllers
{
    //[Authorize]
    public class TermController : Controller
    {
        private QLSVContext db = new QLSVContext();

        //
        // GET: /Term/

        public ViewResult Index()
        {
            return View(db.Tb_Config.Where(g=>g.option.Equals("term")).ToList());
        }
        //
        // POST: /Term/Create

        [HttpPost]
        public ActionResult Create(String displayword, String start, String end)
        {
            if (ModelState.IsValid)
            {
                Tb_Config tb_config = new Tb_Config();
                tb_config.option = "term";
                tb_config.keyword = TimeTableManagement.Models.Functions.RandomPassword(10);
                tb_config.displayword = displayword;
                var list = db.Tb_Config.Where(g => g.option.Equals("term"));
                tb_config.value = (list.ToList().Count + 1).ToString();
                db.Tb_Config.Add(tb_config);
                db.SaveChanges();

                Tb_Config tb_config_start = new Tb_Config();
                tb_config_start.displayword = start;
                tb_config_start.option = "termconfig";
                tb_config_start.keyword = "startterm";
                var l = db.Tb_Config.Where(g => g.keyword.Equals(tb_config.keyword)).Where(g => g.displayword.Equals(tb_config.displayword));
                tb_config_start.value = (l.First().id).ToString();
                db.Tb_Config.Add(tb_config_start);
                
                Tb_Config tb_config_end = new Tb_Config();
                tb_config_end.displayword = end;
                tb_config_end.option = "termconfig";
                tb_config_end.keyword = "endterm";
                tb_config_end.value = tb_config_start.value.ToString();
                db.Tb_Config.Add(tb_config_end);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View();
        }
        
        //
        // POST: /Term/Edit/5

        [HttpPost]
        public ActionResult Edit(String displayword, int id, string start, string end)
        {
            if (ModelState.IsValid)
            {
                var T = db.Tb_Config.Find(id);
                T.displayword = displayword;
                string intconvert = id.ToString();
                var G = db.Tb_Config.Where(g => g.value.Equals(intconvert))
                    .Where(g => g.option.Equals("termconfig"))
                    .Where(g => g.keyword.Equals("startterm")).First();
                var T2 = db.Tb_Config.Find(G.id);
                T2.displayword = start;

                var H = db.Tb_Config.Where(g => g.value.Equals(intconvert))
                    .Where(g => g.option.Equals("termconfig"))
                    .Where(g => g.keyword.Equals("endterm")).First();
                var T3 = db.Tb_Config.Find(H.id);
                T3.displayword = end;

                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Term/Delete/5

        public ActionResult Delete(int id)
        {
            Tb_Config tb_config = db.Tb_Config.Find(id);
            return View(tb_config);
        }

        //
        // POST: /Term/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Tb_Config tb_config = db.Tb_Config.Find(id);
            db.Tb_Config.Remove(tb_config);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        public ActionResult Config()
        {
            //gettermactive
            var T = db.Tb_Config.Where(g => g.keyword.Equals("termactive")).First();
            int termactive = Int32.Parse(T.value);
            ViewBag.hkactive = termactive;

            var H = db.Tb_Config.Where(g => g.keyword.Equals("termrepair")).First();
            int termrepair = Int32.Parse(H.value);
            ViewBag.hkactive = termrepair;
            ViewBag.stringactive = db.Tb_Config.Find(termactive).displayword;
            ViewBag.stringprepair = db.Tb_Config.Find(termrepair).displayword;
            ViewBag.active = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword", termactive);
            ViewBag.prepair = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword", termrepair);
            return View();
        }
        [HttpPost]
        public ActionResult Config(string active, string prepair)
        {
            var T = db.Tb_Config.Where(g => g.keyword.Equals("termactive")).First();
            int termactive = Int32.Parse(T.value);
            ViewBag.hkactive = termactive;

            var H = db.Tb_Config.Where(g => g.keyword.Equals("termrepair")).First();
            int termrepair = Int32.Parse(H.value);
            
            ViewBag.hkactive = termrepair;
            ViewBag.stringactive = db.Tb_Config.Find(termactive).displayword;
            ViewBag.stringprepair = db.Tb_Config.Find(termrepair).displayword;
            ViewBag.active = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword", termactive);
            ViewBag.prepair = new SelectList(db.Tb_Config.Where(g => g.option.Equals("term")), "id", "displayword", termrepair);
            
            if (ModelState.IsValid)
            {
                var AC = db.Tb_Config.Find(T.id);
                AC.value = active;
                var PR = db.Tb_Config.Find(H.id);
                PR.value = prepair;
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
    public class Term
    {
        public String getTermStart(int idterm)
        {
            try
            {
                QLSVContext db = new QLSVContext();
                String tem = idterm.ToString();
                var item = db.Tb_Config
                    .Where(g => g.value.Equals(tem))
                    .Where(h => h.keyword.Equals("startterm"));

                int r = item.First().id; // item.First().id;
                var d = db.Tb_Config.Find(r);
                return d.displayword;
            }
            catch { }
            return "";
        }
        public String getTermEnd(int idterm)
        {
            try
            {
                QLSVContext db = new QLSVContext();
                String tem = idterm.ToString();
                var item = db.Tb_Config
                    .Where(g => g.value.Equals(tem))
                    .Where(h => h.keyword.Equals("endterm"));

                int r = item.First().id; // item.First().id;
                var d = db.Tb_Config.Find(r);
                return d.displayword;
            }
            catch { }
            return "";
        }
        public String getStartReg(int idterm)
        {
            try
            {
                QLSVContext db = new QLSVContext();
                String tem = idterm.ToString();
                var item = db.Tb_Config
                    .Where(g => g.value.Equals(tem))
                    .Where(h => h.keyword.Equals("startreg"));

                int r = item.First().id; // item.First().id;
                var d = db.Tb_Config.Find(r);
                return d.displayword;
            }
            catch { }
            return "";
        }
        public String getEndReg(int idterm)
        {
            try
            {
                QLSVContext db = new QLSVContext();
                String tem = idterm.ToString();
                var item = db.Tb_Config
                    .Where(g => g.value.Equals(tem))
                    .Where(h => h.keyword.Equals("endreg"));

                int r = item.First().id; // item.First().id;
                var d = db.Tb_Config.Find(r);
                return d.displayword;
            }
            catch { }
            return "";
        }
    }
}